package com.woniuxy.controller;
/*
 * @author 幸琦林
 *
 */

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.ShowCourse;
import com.woniuxy.entity.User;
import com.woniuxy.service.CourseInfoService;
import com.woniuxy.util.CommonUtil;

@RestController
@RequestMapping("/course")
public class CourseShowController {

    @Resource
    CourseInfoService courseInfoService;

    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public ShowCourse[][] listAll(Long id) {
        ShowCourse[][] showCourse = courseInfoService.getShowCourse(id);
        Calendar calendar = Calendar.getInstance();
        int i = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        ShowCourse[][] temp = new ShowCourse[7][];
        for (int j = 0; j < 7; j++) {
            temp[j] = showCourse[i];
            i++;
            if (i == 7) {
                i = 0;
            }
        }
        return temp;
        
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ResponseEntity<Void> addCourse(String name,double price){
    	ResponseEntity<Void> re = new ResponseEntity<Void>();
    	User user = CommonUtil.getCurrentUser();
    	try {
    		int res = courseInfoService.addCourse(user.getId(),name,price);
            if (res == 3) {
                re.setCode(3);
                return re;
            }
            re.setCode(1);
        } catch (Exception e) {
            e.printStackTrace();
            re.setCode(2);
        }
        return re;
    }

    @RequestMapping(value = "/chose", method = RequestMethod.GET)
    public Map<String, Object> choseCourse(Long courseId) {
        System.out.println("/course/chose:" + courseId);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg", "选课失败");
        if ("1".equals(CommonUtil.getCurrentUser().getRole())) {
            courseInfoService.choseCourse(courseId, CommonUtil.getCurrentUser().getId());
            map.put("code", 10);
            map.put("msg", CommonUtil.getCurrentUser().getNickname() + "选课成功!");
        } else {
            map.put("code", 500);
            map.put("msg", "不是学员");
        }
        return map;
    }

}
